import pygame
import math
from tank import Tank, Bullet  # Assuming these classes are defined in tank.py

# test

# Constants
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
TILE_SIZE = 100
WALL_THICKNESS = 5
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
NUM_BOUNCES = 5
BULLET_SPEED = 10

# Fixed Maze Definition
FIXED_MAZE = [
    [1, 1, 1, 1, 1, 1, 1, 1],
    [1, 0, 0, 0, 0, 0, 0, 1],
    [1, 0, 0, 1, 1, 0, 0, 1],
    [1, 0, 0, 0, 0, 1, 0, 1],
    [1, 0, 0, 0, 0, 0, 0, 1],
    [1, 1, 1, 1, 1, 1, 1, 1]
]

def draw_maze(screen, maze, tile_size):
    for i, row in enumerate(maze):
        for j, tile in enumerate(row):
            if tile:
                pygame.draw.rect(screen, BLACK, (j * tile_size, i * tile_size, tile_size, tile_size))

def find_starting_position(maze, tile_size):
    for i, row in enumerate(maze):
        for j, tile in enumerate(row):
            if tile == 0:
                return j * tile_size + tile_size // 2, i * tile_size + tile_size // 2
    return tile_size // 2, tile_size // 2

def check_collision(rect, maze, tile_size):
    for i, row in enumerate(maze):
        for j, tile in enumerate(row):
            if tile:
                wall_rect = pygame.Rect(j * tile_size, i * tile_size, tile_size, tile_size)
                if rect.colliderect(wall_rect):
                    return True, wall_rect
    return False, None

def main():
    pygame.init()
    screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
    pygame.display.set_caption("Tank Battle")
    clock = pygame.time.Clock()

    # Load tank image
    tank_image = pygame.image.load("weitank.png")
    tank_image = pygame.transform.scale(tank_image, (50, 50))

    # Use the fixed maze
    maze = FIXED_MAZE

    # Find starting position for the tank
    start_x, start_y = find_starting_position(maze, TILE_SIZE)

    # Create tank
    tank = Tank(start_x, start_y, tank_image)

    bullets = []

    running = True
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
                mouse_x, mouse_y = pygame.mouse.get_pos()
                bullet = Bullet(tank.rect.centerx, tank.rect.centery, mouse_x, mouse_y)
                bullets.append(bullet)

        keys = pygame.key.get_pressed()
        if keys[pygame.K_w]:
            tank.move('up')
            if check_collision(tank.rect, maze, TILE_SIZE)[0]:
                tank.move('down')
        if keys[pygame.K_s]:
            tank.move('down')
            if check_collision(tank.rect, maze, TILE_SIZE)[0]:
                tank.move('up')
        if keys[pygame.K_a]:
            tank.move('left')
            if check_collision(tank.rect, maze, TILE_SIZE)[0]:
                tank.move('right')
        if keys[pygame.K_d]:
            tank.move('right')
            if check_collision(tank.rect, maze, TILE_SIZE)[0]:
                tank.move('left')

        screen.fill(WHITE)
        draw_maze(screen, maze, TILE_SIZE)
        tank.draw(screen)

        for bullet in bullets:
            bullet.move()
            collision, wall_rect = check_collision(bullet.rect, maze, TILE_SIZE)
            if collision:
                bullet.bounces += 1
                if bullet.bounces >= NUM_BOUNCES:
                    bullets.remove(bullet)
                else:
                    # Determine the reflection angle based on the collision side
                    if wall_rect:
                        if bullet.rect.right > wall_rect.left and bullet.rect.left <= wall_rect.right:
                            bullet.angle = math.pi - bullet.angle  # Reflect vertically
                        if bullet.rect.bottom >= wall_rect.top and bullet.rect.top <= wall_rect.bottom:
                            bullet.angle = math.pi - bullet.angle  # Reflect horizontally
            bullet.draw(screen)

        pygame.display.flip()
        clock.tick(30)

    pygame.quit()

if __name__ == "__main__":
    main()
